<template>
  <el-dialog :title="title" @closed="methods.closed" width="50%" ref="dialog" v-model="props.modelValue">
    <el-form :model="props.model" :rules="rules" ref="form" label-position="top" :disabled="props.editType === 'view'">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="上级部门">
            <el-tree-select v-model="props.model.parentId" :data="props.treeData" :render-after-expand="false"
              :props="defaultTreeDataProps"></el-tree-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门代码">
            <el-input v-model="props.model.levelCode" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="props.model.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简称">
            <el-input v-model="props.model.shortName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序">
            <el-input v-model="props.model.sort"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-select v-model="props.model.deptType" placeholder="类型">
              <el-option label="单位" :value="1"></el-option>
              <el-option label="部门" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位类型">
            <el-input v-model="props.model.orgType"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="props.model.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="methods.save" :icon="ButtonIcons.finish" v-if="props.editType !== 'view'">
          提交
        </el-button>
        <el-button @click="methods.close" :icon="ButtonIcons.close">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { SysDept } from '~/types/sys/SysDept'
import { EditType, EditTypeText } from '~/types/ui/EditType'
import { DeptService } from '~/service/sys'
import elpUtil from '~/utils/elp'
import { ButtonIcons } from '~/components/xview/button'
import { ref, computed } from 'vue'
import msgUtil from '~/utils/message'
import { defaultTreeDataProps } from '~/utils/elp/TreeDataProps'
import { IDeptTreeData } from '~/types/sys/IDeptTreeNode'

const form = elpUtil.refs.form()
const dialog = elpUtil.refs.dialog()

const props = withDefaults(
  defineProps<{
    editType: EditType
    model: SysDept
    treeData: IDeptTreeData
    modelValue: boolean
  }>(),
  {
    show: false,
    editType: 'view',
    treeData: null,
    modelValue: false
  }
)

const title = computed(() => {
  return EditTypeText[props.editType] + DeptService.mapDeptType(props.model === null ? null : props.model.deptType)
})

const rules = ref({
  name: [
    {
      required: true,
      message: '名称必须录入'
    }
  ]
})

const emits = defineEmits(['closed', 'saved', 'update:modelValue'])

const methods = {
  closed() {
    form!.value.clearValidate()
    emits('closed')
  },
  close() {
    emits('update:modelValue', false)
  },
  save() {
    form!.value.validate(async (result: boolean) => {
      if (!result) {
        msgUtil.warning('验证错误，请检查输入信息')
        return
      }
      if (props.editType === 'insert') {
        await DeptService.insert(props.model)
      } else {
        await DeptService.update(props.model)
      }
      emits('saved')
      msgUtil.success('部门信息已成功保存')
      emits('update:modelValue', false)
    })
  }
}
</script>
